package com.loveazure.ui;

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import com.loveazure.bll.Handler;
import com.loveazure.eo.Course;
import com.loveazure.ui.MainFrame.Status;

public class CoursePanel extends IPanel {

	private JButton registerBtn;
	private JButton returnBtn;
	private JButton importBtn;
	private JButton exportBtn;
	private JButton deleteBtn;
	private CourseModel model;
	private JTable table;

	private Handler handler;

	public CoursePanel() {
		super();
	}

	@Override
	public void iLayout() {
		GridBagLayout gbl = new GridBagLayout();
		gbl.columnWidths = new int[] { 30, 80, 80, 80, 30 };
		gbl.columnWeights = new double[] { 0, 1, 1, 1, 0 };

		gbl.rowHeights = new int[] { 50, 50, 200, 50, 50 };
		setLayout(gbl);
	}

	@Override
	public void iComponent() {
		importBtn = new JButton("导入课程");
		importBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				CourseImportDialog dialog = new CourseImportDialog(handler
						.getFrame(), true);
				dialog.setVisible(true);

				if (dialog.getCourse() != null) {
					model.addRow(dialog.getCourse());
				}
			}
		});
		exportBtn = new JButton("导出课程");
		deleteBtn = new JButton("删除课程");
		deleteBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRow() != -1) {
					int i = JOptionPane.showConfirmDialog(CoursePanel.this,
							"确认删除", "", JOptionPane.OK_CANCEL_OPTION);
					if (i == 0) {
						handler.getOper().deleteCourse(
								model.getRow(table.getSelectedRow()));
						model.removeRow(table.getSelectedRow());
					}
				}
			}
		});
		model = new CourseModel();
		table = new JTable(model);

		JScrollPane pane = new JScrollPane(table);
		registerBtn = new JButton("注册");
		registerBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				boolean result = handler.getOper().registerCourse(
						model.getRow(table.getSelectedRow()));

				if (result) {
					handler.getFrame().switchPanel(Status.CONFIG);
				}
			}
		});
		returnBtn = new JButton("返回");
		returnBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				handler.getFrame().switchPanel(Status.CONFIG);
			}
		});

		add(new JLabel("课程管理："), new GBC(1, 0, 1, 1));
		add(importBtn, new GBC(1, 1, 1, 1));
		add(exportBtn, new GBC(2, 1, 1, 1));
		add(deleteBtn, new GBC(3, 1, 1, 1));
		add(pane, new GBC(1, 2, 3, 1).setFill(GBC.BOTH));
		add(registerBtn, new GBC(1, 3, 1, 1));
		add(returnBtn, new GBC(3, 3, 1, 1));
	}

	@Override
	public void iInit() {
		handler = Handler.getInstance();
		model.removeAll();
		Course[] courses = handler.getOper().getAllCourse();
		if (courses == null || courses.length == 0) {
			return;
		}
		for (Course c : courses) {
			model.addRow(c);
		}
	}
}
